﻿//========================================================= 
/**@file CallbackAppender.h
 * @brief Android下LOG输出
 * 
 * @date 2018-12-13   16:16:56
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_CALLBACKAPPENDER_H_
#define _LIBZHOUYB_CALLBACKAPPENDER_H_
//--------------------------------------------------------- 
#include "../ILogCat.h"

#include <functional>
//--------------------------------------------------------- 
namespace zhou_yb {
namespace extension {
namespace logcat {
//--------------------------------------------------------- 
/// 通过回调接口输出日志
class CallbackAppender : public ILogAppender, public RefObject
{
public:
    typedef std::function<void(int, const char*, const char*)> callback_type;
protected:
    callback_type _callback;
public:
    CallbackAppender(callback_type callback) : _callback(callback) {}
    CallbackAppender() : _callback(nullptr) {}

    callback_type setAppender(callback_type callback)
    {
        callback_type old = _callback;
        _callback = callback;
        return old;
    }
    virtual void append(LogLevel lv, const char* tag, const char* str)
    {
        if(_callback != nullptr)
        {
            _callback(static_cast<int>(lv), tag, str);
        }
    }
};
//--------------------------------------------------------- 
} // namespace logcat
} // namespace extension
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_CALLBACKAPPENDER_H_
//========================================================= 